package com.ym521.emojipack.keyboard.fragment;


import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ym521.emojipack.EmojiPackKeyBoard;
import com.ym521.emojipack.R;
import com.ym521.emojipack.emoji.EmojiList;
import com.ym521.emojipack.emoji.EmojiPack;
import com.ym521.emojipack.emoji.EmojiPackBean;
import com.ym521.emojipack.keyboard.EmoiiPackClickListener;
import com.ym521.emojipack.keyboard.adapter.EmojiPackGroupAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ym
 */
public final class EmojiPackFragment extends Fragment {
    private EmoiiPackClickListener listener;
    private EmojiPackGroupAdapter emojiPackGroupAdapter;
    private int theme;

    public static EmojiPackFragment newInstance(int theme) {
        EmojiPackFragment packFragment = new EmojiPackFragment();
        packFragment.setEmojiTheme(theme);
        return packFragment;
    }

    public void setEmojiTheme(int theme) {
        this.theme = theme;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.emojipack_item_fragment, container, false);
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        emojiPackGroupAdapter = new EmojiPackGroupAdapter();
        RecyclerView rlvEmojiPack = view.findViewById(R.id.rlvEmojiPack);
        rlvEmojiPack.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false));
        emojiPackGroupAdapter = new EmojiPackGroupAdapter();
        emojiPackGroupAdapter.setListener(listener);
        rlvEmojiPack.setAdapter(emojiPackGroupAdapter);
        rlvEmojiPack.setItemAnimator(null);
        switch (theme) {
            case 1:
                delayedLoad(view);
                break;
            default:
                delayedLoadData(view);
                break;
        }
    }

    private void delayedLoadData(View view) {
        view.postDelayed(() -> {
            List<EmojiPackBean> dataList = new ArrayList<>();
            dataList.add(new EmojiPackBean("emoji表情", EmojiPack.getEmoPacks(EmojiPack.TYPE_PEOPLE)));
            dataList.add(new EmojiPackBean("nature表情", EmojiPack.getEmoPacks(EmojiPack.TYPE_NATURE)));
            dataList.add(new EmojiPackBean("place表情", EmojiPack.getEmoPacks(EmojiPack.TYPE_PLACES)));
            dataList.add(new EmojiPackBean("object表情", EmojiPack.getEmoPacks(EmojiPack.TYPE_OBJECTS)));
            emojiPackGroupAdapter.addAllData(dataList, false);
        }, 250);
    }

    private void delayedLoad(View view) {
        view.postDelayed(() -> {
            List<EmojiPackBean> dataList = new ArrayList<>();
            dataList.add(new EmojiPackBean("emoji表情", EmojiList.DATA));
            emojiPackGroupAdapter.addAllData(dataList, false);
        }, 210);
    }

    @Override
    public void onDetach() {
        listener = null;
        super.onDetach();
    }

    public void setEmojiClickedListener(EmoiiPackClickListener listener) {
        this.listener = listener;
    }
}
